﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Telerik.Web.UI;

public partial class Posts_Tree : BaseUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            listVersion.DataBind();
            if (PostPage.SelectedVersion != null)
            {
                if (PostPage.SelectedVersion.Value < DateTime.Now)
                {
                    listVersion.Items[listVersion.Items.Count - 1].Selected = true;
                }
                else
                {
                    ListItem tmp = listVersion.Items.FindByValue(PostPage.SelectedVersion.Value.ToString("yyyy/MM/dd HH:mm:ss"));
                    if (tmp != null)
                    {
                        tmp.Selected = true;
                    }
                }
            }
            pnlPublishKey.Visible = PostPage.SelectedVersion == null;
        }
    }

    protected BasePostPage PostPage
    {
        get
        {
            var tmp = Page as BasePostPage;
            if (tmp == null)
            {
                throw new ApplicationException("Page does not inherit from BasePostPage");
            }
            return tmp;
        }
    }

    protected void listVersion_DataBinding(object sender, EventArgs e)
    {
        listVersion.Items.Add(new ListItem("Pending", ""));
        listVersion.DataSource = Acms.Bll.SiteVersion.GetAllFuture(Page.AcmsContext.SiteId);
    }

    protected void listVersion_DataBound(object sender, EventArgs e)
    {
        listVersion.Items.Add(new ListItem("Live", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
    }

    protected void listVersion_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listVersion.SelectedValue == "")
        {
            pnlPublishKey.Visible = true;
        }
        else
        {
            pnlPublishKey.Visible = false;
        }
        tvSiteMap.DataBind();
    }

    protected void tvSiteMap_DataBinding(object sender, EventArgs e)
    {
        dsSiteMap.SiteId = PostPage.AcmsContext.SiteId;
        if (string.IsNullOrEmpty(listVersion.SelectedValue))
        {
            dsSiteMap.CurrentSiteVersion = null;
        }
        else
        {
            dsSiteMap.CurrentSiteVersion = DateTime.Parse(listVersion.SelectedValue);
        }
    }

    protected void tvSiteMap_ContextMenuItemClick(object sender, Telerik.Web.UI.RadTreeViewContextMenuEventArgs e)
    {
        if (e.MenuItem.Value == "Copy")
        {
            int id = 0;
            try
            {
                id = int.Parse(e.Node.Value);
            }
            catch
            {
            }

            if (id > 0)
            {
                Post post = Post.GetById(Page.AcmsContext.SiteId, id, null);
                if (post != null)
                {
                    Post copy = Post.Copy(post, true);
                    AddCopiedNode(copy, e.Node.ParentNode);
                }
            }
        }
        else if (e.MenuItem.Value == "Details")
        {
            string url = string.Format("Details.aspx?Id={0}&SV={1}", e.Node.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
        else if (e.MenuItem.Value == "AddChild")
        {
            string url = string.Format("Create.aspx?Id={0}&SV={1}", e.Node.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
        else if (e.MenuItem.Value == "AddSibling")
        {
            string url = string.Format("Create.aspx?Id={0}&SV={1}", e.Node.ParentNode.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
        else if (e.MenuItem.Value == "Publish")
        {
            string url = string.Format("Publish.aspx?Id={0}&SV={1}", e.Node.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
        else if (e.MenuItem.Value == "Position")
        {
            string url = string.Format("Position.aspx?Id={0}&SV={1}", e.Node.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
    }

    private void AddCopiedNode(Post copy, RadTreeNode parentNode)
    {
        RadTreeNode node = new RadTreeNode(copy.DisplayName, copy.Id.ToString());
        node.NavigateUrl = string.Format("Details.aspx?Id={0}&SV=", copy.Id);
        parentNode.Nodes.Add(node);
        foreach (Post child in copy.Children)
        {
            AddCopiedNode(child, node);
        }
    }

    protected void tvSiteMap_DataBound(object sender, EventArgs e)
    {
        pnlNoPendingPosts.Visible = pnlNoPosts.Visible = false;
        if (tvSiteMap.Nodes.Count > 0)
        {
            tvSiteMap.Nodes[0].Expanded = true;
        }
        else
        {
            if (string.IsNullOrEmpty(listVersion.SelectedValue))
            {
                pnlNoPendingPosts.Visible = true;
            }
            else
            {
                pnlNoPosts.Visible = true;
            }
        }
    }

    protected void tvSiteMap_NodeDataBound(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
    {
        Post post = e.Node.DataItem as Post;
        if (post != null)
        {
            e.Node.NavigateUrl = string.Format("Details.aspx?Id={0}&SV={1}", post.Id, listVersion.SelectedValue);
            if (listVersion.SelectedValue == "")
            {
                e.Node.EnableContextMenu = true;
            }
            else
            {
                e.Node.EnableContextMenu = false;
            }
            if (e.Node.Value == PostPage.PostId.ToString())
            {
                e.Node.ExpandParentNodes();
                e.Node.Selected = true;
            }
            if (string.IsNullOrEmpty(listVersion.SelectedValue))
            {
                if (post.ReadyToPublish)
                {
                    e.Node.SelectedCssClass = e.Node.CssClass = "publishReady";
                }
                else if (post.RequiresPublishing)
                {
                    e.Node.SelectedCssClass = e.Node.CssClass = "publishRequired";
                }
                else
                {
                    e.Node.SelectedCssClass = e.Node.CssClass = "publishNormal";
                }
            }
            else
            {
                DateTime tmp = DateTime.Parse(listVersion.SelectedValue);
                if (tmp >= DateTime.Now)
                {
                    if (post.PublishFrom == tmp)
                    {
                        e.Node.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
            }
        }
    }

}
